package com.example.shushiyunsst.fragment.main.mine.child

import android.os.Bundle
import android.text.Editable
import android.view.View
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseDataBindingFragment
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.OnTextListener
import com.example.shushiyunsst.R
import com.example.shushiyunsst.databinding.ReSettingSignInPasswordFragmentBinding
import com.example.wallet.db.UserInfoManager
import com.lxj.xpopup.XPopup
import java.util.regex.Pattern

/**
 * 修改登录密码
 * */
class ReSettingSignInPasswordFragment :
    BaseDataBindingFragment<ReSettingSignInPasswordFragmentBinding>(), ReSettingPassWordLab,
    View.OnClickListener, OnTextListener {

    override val labInfo = "登录密码"

    companion object {
        fun newInstance(): ReSettingSignInPasswordFragment {
            val args = Bundle()

            val fragment = ReSettingSignInPasswordFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var oldPassword = ""

    private var newPassword = ""

    private var finalPassword = ""

    private var vailCode = ""


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport=false
    }

    override fun layoutId(): Int {
        return R.layout.re_setting_sign_in_password_fragment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinder.onViewClick = this
        viewBinder.onTextListener = this
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btnChange -> {
                if (checkInfo()) {
                    sending()
                }
            }
        }
    }

    private fun sending() {
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val map = mapOf(
            "newPasswd" to newPassword,
            "oldPasswd" to oldPassword,
            "reNewPasswd" to finalPassword,
            "type" to 1,
            "uid" to userInfo?.uid
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.updatePasswd)
            .success {
                showPop()
            }.failure {
                Toast.makeText(_mActivity, "发送失败", Toast.LENGTH_SHORT).show()
            }.error { _, msg ->
                Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show()
            }.build().post()
    }

    private fun showPop() {
        XPopup.Builder(context)
            .asCustom(ReSettingSignInPasswordPop(_mActivity))
            .show()
    }

    private fun checkInfo(): Boolean {
        var flag = true
        if (newPassword.isEmpty()) {
            Toast.makeText(_mActivity, "新密码不能为空", Toast.LENGTH_SHORT).show()
            flag = false
        } else if (newPassword != finalPassword) {
            Toast.makeText(_mActivity, "新密码与确认密码不一致", Toast.LENGTH_SHORT).show()
            flag = false
        } else if (!viewBinder.mVerifyCode.isEqualsIgnoreCase(vailCode)) {
            Toast.makeText(_mActivity, "验证码错误", Toast.LENGTH_SHORT).show()
            flag = false
        }

        return flag
    }


    override fun afterTextChanged(s: Editable?, viewId: Int) {
        when (viewId) {
            R.id.oldPassword -> {
                oldPassword = s.toString()
            }
            R.id.newPassword -> {
                newPassword = s.toString()
            }
            R.id.finalPassword -> {
                finalPassword = s.toString()
            }
            R.id.vailCode -> {
                vailCode = s.toString()
            }
        }
    }


}